package com.example.myproject;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;

/**
 * @author sejung
 * upload file to blobstore 
 */
@SuppressWarnings("serial")
public class BlobstoreUploadServlet extends HttpServlet {
	
	private BlobstoreService blobService = BlobstoreServiceFactory
			.getBlobstoreService();
	private final BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
	private UploadDAO dao = new UploadDAO();


	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
				
		Map<String, BlobKey> blobs = blobService.getUploadedBlobs(req);
		
		BlobKey blobKey = blobs.get("myFile");
		
		//to distinguish file upload and thumbnail upload 
		String uploadType = req.getParameter("uploadType")==null?"":req.getParameter("uploadType");
		//master file type for thumbnail 
		String fileType = req.getParameter("fileType")==null?"":req.getParameter("fileType");
		
		String title = req.getParameter("title");
		String caption = req.getParameter("caption");
		//master file id for thumbnail
		Long parentId = Long.parseLong(req.getParameter("parentId")==null?"0":req.getParameter("parentId"));

		
		
		if(blobKey == null)
		{
			resp.sendRedirect("/error");
		}
		else
		{
			BlobInfo info = blobInfoFactory.loadBlobInfo(blobKey);
			FileService fileService = FileServiceFactory.getFileService();
			AppEngineFile file = fileService.getBlobFile(blobKey);
			
			if(uploadType.equals("thumbnail"))
			{
				ThumbnailUpload thumbnail = new ThumbnailUpload();
				ThumbnailUploadDTO thumbnailDTO = new ThumbnailUploadDTO();
				
				thumbnailDTO.setPath(blobKey.getKeyString()); 
				thumbnailDTO.setParentId(parentId);
				thumbnailDTO.setType(fileType);
				
				thumbnail.upload(thumbnailDTO);
	 		
			}
			else
			{
				/* save master google datastore */
				UploadDTO master = new UploadDTO();
				master.setTitle(title);
				master.setContentType(info.getContentType());
				master.setPath(blobKey.getKeyString()); 
				master.setCaption(caption); 
				dao.save(master);
		
				
				File src = new File(file.getFullPath());
				
				/* music file upload */
				if (info.getContentType().contains("audio")) 
				{
					AudioUpload audio = new AudioUpload();
					
					if(!audio.upload(master, req.getParameter("genre"), req.getParameter("album"))) 
					{
						resp.sendRedirect("/error");
						return;
					}
						
				}
				/* video file upload */
				else if (info.getContentType().contains("video")) 
				{
					VideoUpload video = new VideoUpload();
					if(!video.upload(master, req.getParameter("episode"), req.getParameter("series")))
					{
						resp.sendRedirect("/error");
						return;
					}
				}
				/* image file upload */
				else if (info.getContentType().contains("image")) 
				{
					PictureUpload picture = new PictureUpload();
					if(!picture.upload(src, master))
					{
						resp.sendRedirect("/error");
						return;
					}
				}
				/* book file upload */
				else if (info.getContentType().contains("pdf") || info.getContentType().contains("epub") )
				{
					BookUpload book = new BookUpload();
					if(!book.upload(src, master))
					{
						resp.sendRedirect("/error");
						return;
					}
				}
				
			}
			resp.sendRedirect("index.jsp");
			
		}
	}

}